package com.lezhu.plcsdklibrary;

import java.math.BigInteger;

/**
 * 描述：常量类
 * 作者: 钮家齐
 * 时间: 2020/5/23 16:30
 */
public class Constants {


    public static final int BaudRate = 9600;            //波特率

    public static final String PLC_address = "0A";           //PLC硬件地址

    public static String Protocol = "";              //功能吗
    public static String length = "";              //长度

    public static final String Open = "FF00";           //开门指令

    public static final String[] doorList =
            {"1号柜门",
                    "2号柜门",
                    "3号柜门",
                    "4号柜门",
                    "5号柜门",
                    "6号柜门",
                    "7号柜门",
                    "8号柜门",
                    "9号柜门",
                    "10号柜门",
                    "11号柜门",
                    "12号柜门",
                    "13号柜门",
                    "14号柜门",
                    "15号柜门",
                    "16号柜门",
                    "17号柜门",
                    "18号柜门",
                    "19号柜门",
                    "20号柜门",
                    "21号柜门",
                    "22号柜门",
                    "23号柜门",
                    "24号柜门",
                    "25号柜门",
                    "26号柜门",
                    "27号柜门",
                    "28号柜门",
                    "29号柜门",
                    "30号柜门",
                    "31号柜门",
                    "32号柜门",
                    "33号柜门",
                    "34号柜门",
                    "35号柜门",
                    "36号柜门",
                    "37号柜门",
                    "38号柜门",
                    "39号柜门",
                    "40号柜门",
                    "41号柜门",
                    "42号柜门",
                    "43号柜门",
                    "44号柜门",
                    "45号柜门",
                    "46号柜门",
                    "47号柜门",
                    "48号柜门",
                    "49号柜门",
                    "50号柜门",
                    "51号柜门",
                    "52号柜门",
                    "53号柜门",
                    "54号柜门",
                    "55号柜门",
                    "56号柜门",
                    "57号柜门",
                    "58号柜门",
                    "59号柜门",
                    "60号柜门",
                    "查询整机状态",
                    "重启主板",
                    "查询5号门状态"};

    /**
     * 获取开门命令
     *
     * @param pos 几号柜
     * @return
     */
    public static String getCMD(int pos) {
        int position = pos - 1;
        String door = "";
        String cmd = "";
        if (position < 60) {
            Protocol = "02";
            length = "0800";
            if (position < 10) {
                door = "" + position;
            } else {
                door = "" + position;
            }
            //拼接开门命令
            String s = Integer.toHexString(Integer.parseInt(door));
            if (s.length() == 1) {
                s = "0" + s;
            }

            cmd = PLC_address + Protocol + length + s;
        }


        if (position == 60) {
            //查询整机状态
            cmd = "0A04080001";
        }
        if (position == 61) {
            //重启主板
            cmd = "0A42080042";
        }
        if (position == 62) {
            //查询5号门状态
            cmd = "0A03080005";
        }
        OperatListUtls.getInstance().addLog("开门指令==" + cmd);

        String tempcmd = cmd + CRC16Util.getTableCRC(new BigInteger(cmd, 16).toByteArray()) + "40";
        //追加crc16校验
//        return cmd + CRC16Util.getTableCRC(new BigInteger(cmd, 16).toByteArray()) + "40";
        return tempcmd;
    }

    public static String hexString2binaryString(String hexString) {
        if (hexString == null || hexString.length() % 2 != 0)
            return null;
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }


    /**
     * 查询所有柜门状态
     *
     * @return
     */
    public static String getAllcmd() {

//        if (Commons.isOld) {
//            String readDoorStatus = Constantsold.readDoorStatus();
//            return readDoorStatus;
//        }
        return getCMD(60);
    }


    //查询指定柜门状态
    public static String getIdnexCmd(int index) {
//        String cmd = "0A03080005";
        String cmd = "0A030800";
        String door = "";

        door = "" + index;
        //拼接开门命令,柜子序列号要转成16进制两位,不足两位补0
        String s = Integer.toHexString(Integer.parseInt(door));
        if (s.length() == 1) {
            s = "0" + s;
        }
        cmd = cmd + s;
        return cmd + CRC16Util.getTableCRC(new BigInteger(cmd, 16).toByteArray()) + "40";


    }
}
